package com.hue.springbootjwt.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hue.springbootjwt.domain.User;
import com.hue.springbootjwt.mapper.UserMapper;
import com.hue.springbootjwt.service.UserService;
import com.hue.springbootjwt.utils.JwtUtils;
import com.hue.springbootjwt.utils.RedisUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public String login(User user) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try {
            subject.login(token);
        } catch (Exception e) {
            return null;
        }
        String jwt = JwtUtils.login(user);
        redisUtils.login(jwt);
        return jwt;
    }
}
